package com.bh.order.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bh.common.constant.Entity;
import com.bh.common.utils.DateUtils;
import com.bh.common.utils.SecurityUtils;
import com.bh.ledger.domain.JcLedgerDetail;
import com.bh.ledger.mapper.JcLedgerDetailMapper;
import com.bh.order.domain.*;
import com.bh.order.mapper.*;
import com.bh.order.service.IJcPrescoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 计划单Service业务层处理
 *
 * @author slx
 * @date 2023-04-24
 */
@Service
public class JcPrescoServiceImpl extends ServiceImpl<JcPrescoMapper, JcPresco> implements IJcPrescoService {
    @Autowired
    private JcPrescoMapper jcPrescoMapper;
    @Autowired
    private JcOrderMapper jcOrderMapper;
    @Autowired
    private JcOrderReceivingPartyMapper orderReceivingPartyMapper;
    @Autowired
    private JcOrderProductMapper orderProductDao;
    @Autowired
    private JcPrescoProductMapper prescoProductDao;
    @Autowired
    private JcLedgerDetailMapper ledgerDetailDao;
    @Autowired
    private JcLedMapper ledDao;


    /**
     * 查询计划单
     *
     * @param id 计划单主键
     * @return 计划单
     */
    @Override
    public JcPresco selectJcPrescoById(Long id) {
        return jcPrescoMapper.selectById(id);
    }

    /**
     * 查询计划单列表
     *
     * @param jcPresco 计划单
     * @return 计划单
     */
    @Override
    public List<JcPresco> selectJcPrescoList(JcPresco jcPresco) {
        return jcPrescoMapper.selectJcPrescoList(jcPresco);
    }

    /**
     * 新增计划单
     *
     * @param jcPresco 计划单
     * @return 结果
     */
    @Override
    public int insertJcPresco(JcPresco jcPresco) {
        jcPresco.setCreateTime(DateUtils.getNowDate());
        jcPresco.setCreateName(SecurityUtils.getUsername());
        jcPresco.setStatus(Entity.ACTIVE);
        return jcPrescoMapper.insert(jcPresco);
    }

    /**
     * 修改计划单
     *
     * @param jcPresco 计划单
     * @return 结果
     */
    @Override
    public int updateJcPresco(JcPresco jcPresco) {
        jcPresco.setModifyName(SecurityUtils.getUsername());
        jcPresco.setModifyTime(DateUtils.getNowDate());
        return jcPrescoMapper.updateById(jcPresco);
    }

    /**
     * 批量删除计划单
     *
     * @param ids 需要删除的计划单主键
     * @return 结果
     */
    @Override
    public int deleteJcPrescoByIds(Long[] ids) {
        return jcPrescoMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除计划单信息
     *
     * @param id 计划单主键
     * @return 结果
     */
    @Override
    public int deleteJcPrescoById(Long id) {
        return jcPrescoMapper.deleteById(id);
    }

    @Override
    public int confirmationOrder(JcPresco presco) {
        JcOrder order = new JcOrder();
        order.setCustomerType(presco.getCustomerType());
        if (presco.getJcCustomerId() != null) {
            order.setJcCustomerId(presco.getJcCustomerId());
            order.setFeeType(0);
        }
        if (presco.getRelateBill1() != null) {
            order.setRelateBill1(presco.getRelateBill1());
        }
        order.setTransportPro(0);
        order.setOrderMileage(0d);
        order.setIsTake(0);
        order.setJcOrganizationId(presco.getJcOrganizationId());
        order.setCode(presco.getCode());
        order.setCustomerName(presco.getCustomerNameLs());
        order.setJcPrescoId(presco.getId());
        order.setCreateName(SecurityUtils.getUsername());
        order.setCreateTime(DateUtils.getNowDate());
        order.setStatus(Entity.ACTIVE);
        jcOrderMapper.insert(order);
        //插入发货方
        JcOrderReceivingParty fh = new JcOrderReceivingParty();
        fh.setName(presco.getFhName());
        fh.setContactperson(presco.getFhPerson());
        fh.setIphone(presco.getFhIphone());
        fh.setAddress(presco.getFhAddress());
        fh.setLtl(presco.getFhLtl());
        fh.setJcOrderId(order.getId());
        fh.setType(0);
        fh.setDetaileAddress(presco.getFhDetaileaddress());
        fh.setStatus(Entity.ACTIVE);
        fh.setCreateName(SecurityUtils.getUsername());
        fh.setCreateTime(DateUtils.getNowDate());
        orderReceivingPartyMapper.insert(fh);
        //插入收货方
        if (presco.getShName() != null) {
            JcOrderReceivingParty sh = new JcOrderReceivingParty();
            sh.setName(presco.getShName());
            sh.setContactperson(presco.getShPerson());
            sh.setIphone(presco.getShIphone());
            sh.setAddress(presco.getShAddress());
            sh.setLtl(presco.getShLtl());
            sh.setJcOrderId(order.getId());
            sh.setType(1);
            sh.setDetaileAddress(presco.getShDetaileaddress());
            sh.setStatus(Entity.ACTIVE);
            sh.setCreateName(SecurityUtils.getUsername());
            sh.setCreateTime(DateUtils.getNowDate());
            orderReceivingPartyMapper.insert(sh);
        }
        //向订单货品明细里插入数据
        List<JcPrescoProduct> prescoProducts = prescoProductDao.selectByPrescoId(presco.getId());
        List<JcOrderProduct> orderProducts = new ArrayList<>();
        for (JcPrescoProduct prescoProduct : prescoProducts) {
            JcOrderProduct orderProduct = new JcOrderProduct();
            orderProduct.setName(prescoProduct.getName());
            orderProduct.setJcOrderId(order.getId());
            orderProduct.setNumber(prescoProduct.getNumber());
            orderProduct.setUnit(prescoProduct.getUnit());
            orderProduct.setValue(prescoProduct.getValue());
            orderProduct.setVolume(prescoProduct.getVolume());
            orderProduct.setWeight(prescoProduct.getWeight());
            orderProduct.setDescription(prescoProduct.getDescription());
            orderProduct.setJzWeight(prescoProduct.getJzWeight());
            orderProduct.setStatus(Entity.ACTIVE);
            orderProduct.setCreateName(SecurityUtils.getUsername());
            orderProduct.setCreateTime(DateUtils.getNowDate());
            orderProducts.add(orderProduct);
        }
        orderProductDao.insertBatch(orderProducts);

        //向台账明细里插入数据
        List<JcLedgerDetail> orderLedgerDetails = new ArrayList<>();
        List<JcLedgerDetail> ledgerDetails = ledgerDetailDao.selectByPrescoId(presco.getId());
        for (JcLedgerDetail ledgerDetail2 : ledgerDetails) {
            JcLedgerDetail ledgerDetail = new JcLedgerDetail();
            ledgerDetail.setInput(ledgerDetail2.getInput());
            ledgerDetail.setAmount(ledgerDetail2.getAmount());
            ledgerDetail.setFeeType(ledgerDetail2.getFeeType());
            ledgerDetail.setTaxrate(ledgerDetail2.getTaxrate());
            ledgerDetail.setType(0);
            ledgerDetail.setCategory(1);
            ledgerDetail.setJcOrderId(order.getId());
            ledgerDetail.setStatus(Entity.ACTIVE);
            ledgerDetail.setCreateName(SecurityUtils.getUsername());
            ledgerDetail.setCreateTime(DateUtils.getNowDate());
            orderLedgerDetails.add(ledgerDetail);
        }
        ledgerDetailDao.insertBatch(orderLedgerDetails);


        List<JcLed> leds = ledDao.selectByPrescoId(presco.getId());
        for (JcLed led : leds) {
            led.setCode(led.getCode() + "已转正");
            led.setRelateBill1(order.getRelateBill1());
            led.setJcToOrganizationId(order.getJcToOrganizationId());
            led.setJcStartZoneId(order.getJcStartZoneId());
            led.setJcEndZoneId(order.getJcEndZoneId());
            led.setJcOrderId(order.getId());
            led.setModifyName(SecurityUtils.getUsername());
            led.setModifyTime(DateUtils.getNowDate());
            ledDao.updateById(led);
        }
        //改变预录单的状态(已转正  100)
        presco.setStatus(100);
        presco.setModifyName(SecurityUtils.getUsername());
        presco.setModifyTime(DateUtils.getNowDate());
        return jcPrescoMapper.updateById(presco);
    }
}
